layui.config({
    base : "/js/"
}).extend({
})

layui.use(['form','layer','mframe'],function(){
    var form = layui.form
        layer = parent.layer === undefined ? layui.layer : top.layer,
        mframe = layui.mframe,
        $ = layui.jquery;

    $(".close").click(function(e){
    	parent.layui.layer.closeAll("iframe");
    })
    
    $(".deptName").click(function(){
    	var treeId = $("#treeId").val();
    	var deptId = treeId=="" ? "100" : $("#treeId").val();
    	var url = ctx + "system/dept/selectDeptTree/" + deptId;
    	var height = $(window).height() - 150;
    	layui.layer.open({
    		type: 2,
			maxmin: true,
			shade: 0.3,
			title: "选择部门",
			fix: false,
			area: [ '480px', height + 'px'],
			content: url,
			shadeClose: true,
			btn: ['确认', '关闭'],
			yes: function (index, layero) {
	    		doSubmit(index,layero)
			}, 
	    	cancel: function () {
	        	return true;
	    	}
    	});
    })
    
    function doSubmit(index, layero){
		var body = layui.layer.getChildFrame('body', index);
		$("#treeId").val(body.find('#treeId').val());
		$("#treeName").val(body.find('#treeName').val());
		layui.layer.close(index);
	}
    form.on("submit(addUser)",function(data){
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        var roles = mframe.getCheckBoxVal("role");
        $.ajax({
    		type : "POST",
    		url : ctx + "system/user/add",
    		data : {
    			"deptId" :data.field.deptId,
    			"loginName": data.field.loginName,
    			"userName": data.field.userName,
    			"password": data.field.password,
    			"email": data.field.email,
    			"phonenumber": data.field.phonenumber,
    			"sex": data.field.sex,
    			"roleIds": roles
    		},
    		success : function(data) {
    			top.layer.close(index);
                top.layer.msg(data.msg);
                //layer.closeAll("iframe");
                //刷新父页面
                parent.location.reload();
    		}
    	});
        return false;
    })
})